package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdUserMapper;
import com.heima.admin.service.AdUserService;
import com.heima.common.constants.admin.AdminConstants;
import com.heima.common.exception.CustException;
import com.heima.model.admin.pojos.AdUser;
import com.heima.model.common.dtos.AdUserDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.vo.AdUserVo;
import com.heima.utils.common.AppJwtUtil;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Jt
 * @date 2021/4/11 16:05
 */
@Service
public class AdUserServiceImpl extends ServiceImpl<AdUserMapper, AdUser> implements AdUserService {

    /**
     * admin登录功能
     * @param adUserDto
     * @return
     */
    @Override
    public ResponseResult login(AdUserDto adUserDto) {
//        1.检查参数
        if (adUserDto == null || StringUtils.isBlank(adUserDto.getName()) || StringUtils.isBlank(adUserDto.getPassword())) {
            CustException.cust(AppHttpCodeEnum.LOGIN_NAME_PASSWORD_NULL);
        }
//        2.根据用户名查询用户
        AdUser user = getOne(Wrappers.<AdUser>lambdaQuery().eq(AdUser::getName, adUserDto.getName()));
//        进行判断
        if (user == null) {
            CustException.cust(AppHttpCodeEnum.LOGIN_NAME_BULL);
        }
//        3.检查status的状态,如果为9才是为正常
        if (user.getStatus() != AdminConstants.AD_USER_ISSTATUS) {
            CustException.cust(AppHttpCodeEnum.LOGIN_STATUS_NO);
        }
//        4.检查密码是否正确
        String inputPwd = DigestUtils.md5DigestAsHex((adUserDto.getPassword() + user.getSalt()).getBytes());
        String password = user.getPassword();
        if (!inputPwd.equals(password)) {
            CustException.cust(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
//        5.修改用户的最近登录时间
        user.setCreatedTime(new Date());
        updateById(user);
//        6.颁发token
        String token = AppJwtUtil.getToken(Long.valueOf(user.getId()));
//        7.封装返回的结果{code:  msg:  data:{token:"" , user:{} } }
        Map map = new HashMap();
        map.put("token",token);
        AdUserVo userVo = new AdUserVo();
        BeanUtils.copyProperties(user,userVo);
        map.put("user",userVo);
        return ResponseResult.okResult(map);
    }

    /**
     * 注册功能
     * @param adUser
     * @return
     */
    @Override
    public ResponseResult register(AdUser adUser) {
//        1.参数校验
        if (StringUtils.isBlank(adUser.getName()) || StringUtils.isBlank(adUser.getPassword())) {
            CustException.cust(AppHttpCodeEnum.USER_NOT_NULL);
        }
//        2.对密码进行加密
        String randomAlphanumeric = RandomStringUtils.randomAlphanumeric(8);
        String password = adUser.getPassword();
        String md5DigestAsHex = DigestUtils.md5DigestAsHex((password + randomAlphanumeric).getBytes());
        adUser.setPassword(md5DigestAsHex);
        adUser.setCreatedTime(new Date());
//        设置状态
        adUser.setStatus(9);
//        设置盐
        adUser.setSalt(randomAlphanumeric);
        save(adUser);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
